<?php
	session_start();
	if(!$_SESSION["username"]){
		//not logged in
		header("location:index.php");
	}
	// Connect to server and select databse.
	mysql_connect("localhost", "root", "")or die("cannot connect"); 
	mysql_select_db("Project 5.5")or die("cannot select DB");
	
	$groupid=-1;
		
	//exports group information into a table
	function listGroups(){
		$sql='SELECT * FROM groups';
		$result=mysql_query($sql);
		$count=mysql_num_rows($result);
		$full=0;
		global $groupid;
		
		echo '<table align="center" border="1" cellspacing="0" width="350">';
		while ($row = mysql_fetch_row($result)){
			$members=explode(',',$row[2]);
			
			echo '<td style="padding:5;font-size:15pt;">'.$row[1].'</td><td style="padding:5">';
			for($i=0;$i<sizeof($members);$i++){
			
				$sql2='SELECT First_Name, Last_Name FROM users WHERE User_ID='.$members[$i];
				$result2=mysql_fetch_row(mysql_query($sql2));
				echo ''.$result2[0].' '.$result2[1].'<br />';
			}
			echo '<td style="padding:5;color:#00F;">';
			if($row[0]==$groupid){
				echo "<a href='leavegroup.php?groupid=".$groupid."'>Withdraw</a><br>";
			} else{
				if(sizeof($members)<5){
					if($groupid==0){
						echo '<a href="joingroup.php?groupid='.$row[0].'"> Join Group </a>';
					}
				}else{
					echo 'Group Full';
					$full++;
				}
			}
			
			echo '</td></tr>';
		}
			
		$result=mysql_query($sql);
		if ((!mysql_fetch_row($result))||($full==$count)){
			echo '</table><table align="center" border="1" cellspacing="0" width="350"><tr><td padding=5>No Free Groups Found, <a href="creategroup.php">Create Group</a></td></tr></table>';
		}else{
			echo '</table>';		
		};
	}
	
	//exports group information
	function myGroup(){		
		
		global $groupid;
		
		$sql="SELECT Group_ID FROM studentinfo WHERE User_ID IN (SELECT User_ID FROM users WHERE Username='".$_SESSION['username']."')";
		$result=mysql_fetch_row(mysql_query($sql));
		$groupid=$result[0];
		
		if($groupid==0){
			echo 'You have no group';
		} else {
			$sql="SELECT * FROM groups WHERE Group_ID=".$groupid;
			$result=mysql_fetch_row(mysql_query($sql));
			$members=explode(',',$result[2]);
			
			$write="";
			echo "<font size=5>".$result[1]."</font>";
			for($i=0;$i<sizeof($members);$i++){
				$sql='SELECT First_Name, Last_Name FROM users WHERE User_ID='.$members[$i];
				$result=mysql_fetch_row(mysql_query($sql));
				$write = $write.$result[0]." ".$result[1].", ";
			}
			$write = rtrim($write, ", ");
			$write = ltrim($write, ", ");
			echo "<center><br>".$write;
			if (sizeof($members)>1) {
				echo "<br><br><a href='peer-review.php?groupid=".$groupid."'>Online Peer Review</a>";
			}
			echo "<br><br><a href='leavegroup.php?groupid=".$groupid."'>Withdraw from Group</a>";
		}
	
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Groups</title>
	<script type="text/javascript" src="js/formsValidate.js"/></script>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
	<div id="topbar">
		<div id="qut_logo" />  
		<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
	</div>
	<div id="menubar">
		<div id="breadcrumbs">
			<a href="index.php">Home</a> \ <a href="groups.php">Groups</a>
		</div>
		<div id="logout"> 
			<a href="login.php?destroy=true">Log Out</a>
		</div>
	</div>
	<div id="namebar_groups"></div>
	<div id="body">
		<br/>
		 
		<?php
				echo '<table id="form4" align="center" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
					<tr><td width="59%" style="padding:15px;vertical-align:top;">
					<div style="font-size:14pt; font-weight:bold; text-decoration:none; padding-bottom:15px; background-color:#FFF; text-align:center">
						<div style="background-color: #CCC;font-size:25pt;font-weight:bold;">
							Your Group
						</div>
						<p>';
				myGroup();
				echo '</p>
					</div>					
				</td>
				<td width="41%" style="padding:15px;vertical-align:top;">';
		?>
					<div style="font-size:13pt; font-weight:200; text-decoration:none; text-align:center; background-color:#FFF;">
						<div style="background-color:#CCC;font-size:25pt;font-weight:bold;">
							Group List
						</div>
						<p>
							<?php listGroups(); ?>
						</p>
					</div>					
				</td>
			</tr>
		</table>
	</div>
	<div id="bottombg">
		<p>
			<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
		</p>
	</div>
</body>
</html>